/*
题目地址:https://leetcode-cn.com/problems/reverse-integer/
题目标签:数学
*/

#include <limits.h>

class Solution {
   public:
    int reverse(int x) {
        int ans = 0;
        while (x != 0) {
            if (INT_MIN / 10 <= ans && ans <= INT_MAX / 10) {
                ans = ans * 10 + x % 10;
                x /= 10;
            } else {
                return 0;
            }
        }
        return ans;
    }
};